package com.code.leetcode.offer;

import com.code.leetcode.offer.tutils.Node;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/8/1 22:41
 */
public class The36_B {

    Node pre, head;

    public Node treeToDoublyList(Node root) {
        if (root == null) return null;
        dfs(root);
        head.left = pre;
        pre.right = head;
        return head;
    }

    public void dfs(Node curr) {
        if (curr == null) return;
        dfs(curr.left);

        if (pre != null) pre.right = curr;
        else head = curr;
        curr.left = pre;
        pre = curr;

        dfs(curr.right);
    }
}
